package com.bzkj.basicinfo.aspect;

import com.bzkj.basicinfo.domain.wms.WmsLogEntity;
import com.bzkj.basicinfo.domain.wms.dto.WmsStoStockDto;
import com.bzkj.basicinfo.mapper.WmsLogMapper;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.utils.SecurityUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @Date 2025/01/02 16:45:06
 * @description 记录操作库存表日志信息
 */
@Aspect
@Component
public class WmsStoStockLogAspect
{
    @Autowired
    private WmsLogMapper wmsLogMapper;

    @Pointcut("execution(* com.bzkj.basicinfo.service.wms.impl.WmsStoStockServiceImpl.setOutboundLimit(..))")
    public void setOutboundLimit()
    {
    }

    @Pointcut("execution(* com.bzkj.basicinfo.service.wms.impl.WmsStoStockServiceImpl.updateOutboundLimit(..))")
    public void updateOutboundLimit()
    {
    }

    @AfterReturning(pointcut = "setOutboundLimit()", returning = "r")
    public void doAfterReturning(R<WmsStoStockDto> r)
    {
        WmsStoStockDto data = r.getData();
        if(! Objects.isNull(data)) {
            WmsLogEntity log = new WmsLogEntity();
            log.setTableName("wms_sto_stock");
            log.setOperator(SecurityUtils.getLoginUser().getUsername());
            log.setOperatingTime(new Date());
            log.setOperationType(2);
            String description = "设置了物料id值为:" + data.getPkMaterial() + "的数据的物料状态为:" + data.getPkStorestate();
            log.setDescription(description);
            r.setData(null);
            wmsLogMapper.save(log);
        }
    }

    @AfterReturning(pointcut = "updateOutboundLimit()")
    public void afterUpdateOutboundLimit()
    {
        String username = SecurityUtils.getLoginUser().getUsername();
        WmsLogEntity log = new WmsLogEntity();
        log.setTableName("wms_sto_stock");
        log.setOperator(username);
        log.setOperatingTime(new Date());
        log.setOperationType(2);
        log.setDescription("用户:" + username + "对:wms_sto_stock表进行了修改");
        wmsLogMapper.save(log);
    }

}
